<?php
$uploaddir = '/var/www/html/socket/socket/app/public/images/message/';
// $site_url = 'http://socket.test/socket/app/';
$site_url = 'http://10.3.141.169/';

if (!empty($_FILES['file']['error'])) {
    echo json_encode(['code' => 2, 'msg' => 'error-'.$_FILES['file']['error'], 'data' => '']);
}else {
    $file_name = basename($_FILES['file']['name']);
    $uploadfile = $uploaddir . $file_name;
    if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
        $arr = explode('.', $file_name);
        $ext = '.'.end($arr);
        $new_name = uniqid().time().$ext;
        $res = rename($uploadfile,$uploaddir.$new_name);
        if ($res) {
            $src = $site_url.'public/images/message/'.$new_name;
            echo json_encode(['code' => 0, 'msg' => 'success', 'data' => ['src' => $src]]);
        }else {
            echo json_encode(['code' => 2, 'msg' => '', 'data' => '']);
        }
    } else {
        echo json_encode(['code' => 2, 'msg' => '', 'data' => '']);
    }
}
